package com.car.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.WoniuException;
import com.car.order.client.CarAccountClient;
import com.car.order.client.CarServeClient;
import com.car.order.entity.form.stockLog.StockLogFindListForm;
import com.car.order.entity.form.stockLog.StockLogFindPageForm;
import com.car.order.entity.form.stockLog.StockLogInsertForm;
import com.car.order.entity.form.stockLog.StockLogUpdateForm;
import com.car.order.mapper.StockLogMapper;
import com.car.order.entity.StockLog;
import com.car.order.service.StockLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杜岷芫
 * @since 2024-08-29 11:49:24
 */
@Service
@Slf4j
public class StockLogServiceImpl extends ServiceImpl<StockLogMapper, StockLog> implements StockLogService {

    @Autowired
    private StockLogMapper stockLogMapper;
    @Resource
    private CarServeClient carServeClient;
    @Resource
    private CarAccountClient carAccountClient;

    @Override
    public Page<StockLog> findPage(StockLogFindPageForm form) {
        Page<StockLog> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?6:form.getPageSize());
        QueryWrapper<StockLog> query = new QueryWrapper<>();
        query.eq("stock_id", form.getStockId());
        Page<StockLog> stockLogPage = stockLogMapper.selectPage(page, query);
        return stockLogPage;
    }

    @Override
    public List<StockLog> findList(StockLogFindListForm form){
        QueryWrapper<StockLog> query = new QueryWrapper<>();
        return stockLogMapper.selectList(query);
    }

    @Override
    public StockLog findById(Integer id) {
        return stockLogMapper.selectById(id);
    }

    @Override
    public StockLog insert(StockLogInsertForm form) {
        StockLog stockLog = BeanUtil.toBean(form, StockLog.class);
        boolean save = save(stockLog);
        if (!save) {
            throw new WoniuException(500, "新增失败");
        }
        return stockLog;
    }

    @Override
    public StockLog update(StockLogUpdateForm form) {
        StockLog stockLog = BeanUtil.toBean(form, StockLog.class);
        boolean b = updateById(stockLog);
        if (!b) {
            throw new WoniuException(500, "修改失败");
        }
        return stockLog;
    }

    @Override
    public void delete(Integer id) {
        Integer i=stockLogMapper.deleteById(id);
        if (i == 0) {
            throw new WoniuException(500, "删除失败");
        }
    }

}